package com.ymttest.database.mongo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.mongodb.WriteResult;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.database.MongoDBDriver;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/************************************************************************************
 * @File name :ULC201406Wapper.java
 * @Author : zhouyi
 * @Date : 2015年6月12日
 * @Copyright : 洋码头
 ************************************************************************************/
public class ULC201406Wapper {
	MongoDBDriver mdd;

	public ULC201406Wapper() throws Exception {
		String[] addresses = EnvSetup.getSystemUrl("set1_mongoip").split(";");
		int port = Integer.valueOf(EnvSetup.getSystemUrl("set1_mongoport"));
		List<Map<String, String>> addresslist = new ArrayList<Map<String, String>>();
		for (String address : addresses) {
			HashMap m = new HashMap();
			m.put("address", address);
			m.put("port", port);
			addresslist.add(m);

		}
		MongoDBDriver.setSystemMongoPoolSize(Integer.valueOf(EnvSetup.getSystemUrl("mongoPoolSize")));
		mdd = new MongoDBDriver(addresslist, "Ymt_Auth_201406");
		mdd.setPoolSize(Integer.valueOf(EnvSetup.getSystemUrl("mongoPoolSize")));
		mdd.auth("authuser", "123456");
		mdd.selectCollection("ULC_201406");
	}

	/**
	 * 用uid查询ULC201406Bean 这个数据是用来保存用户登入信息(密码 登入账号 uid)的
	 * 
	 * @param uid
	 * @return AccessToken
	 * @throws Exception
	 */
	public ULC201406Bean getULCInfoByUid(int uid) throws Exception {
		String re = null;
		ULC201406Bean scmb = null;
		ArrayList<String> list = mdd.selectPartLimit("{\"uId\":" + uid + "}", 1);
		try {
			if (list.size() > 0) {
				re = list.get(0);
				Logger.debug(re.toString());
				scmb = (ULC201406Bean) FormatUtil.GSONToObject(re, ULC201406Bean.class);
				return scmb;
			}
		} catch (Exception e) {
			Logger.debug("err data uid:" + uid);
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param key
	 * @param regex
	 * @return
	 * @throws Exception
	 */
	public List<ULC201406Bean> selectByRegex(String key, String regex, int limit) throws Exception {
		List<ULC201406Bean> re = new ArrayList<ULC201406Bean>();
		ULC201406Bean scmb = null;
		ArrayList<String> list = mdd.selectPartLimit("{\"" + key + "\":{$regex:\"" + regex + "\"}}", limit);
		try {
			for (String ub : list) {
				scmb = (ULC201406Bean) FormatUtil.GSONToObject(ub, ULC201406Bean.class);
				re.add(scmb);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return re;
	}

	/**
	 * @param json
	 * @return
	 * @throws Exception
	 */
	public List<ULC201406Bean> selectByJson(String json, int limit) throws Exception {
		List<ULC201406Bean> re = new ArrayList<ULC201406Bean>();
		ULC201406Bean scmb = null;
		ArrayList<String> list = mdd.selectPartLimit(json, limit);
		try {
			for (String ub : list) {
				scmb = (ULC201406Bean) FormatUtil.GSONToObject(ub, ULC201406Bean.class);
				re.add(scmb);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return re;
	}

	// db.ULC_201406.update({"uId" : 93},{$set:{"lPas" :
	// "F47C010581713BD523BE9B43677C4872"}})
	/**
	 * 修改uid用户的登入密码为8ymatou.com
	 * 
	 * @param uid
	 * @return WriteResult
	 * @throws Exception
	 */
	public WriteResult updatePwdByUid(int uid) throws Exception {
		WriteResult wr = mdd.update("{\"uId\" : " + uid + "}", "{\"lPas\" : \"F47C010581713BD523BE9B43677C4872\"}");
		return wr;
	}
	
	/**
	 * 修改邮箱为希望的值
	 * @param uid
	 * @return
	 * @throws Exception 
	 */
	public WriteResult updateEmail(int uid, String email) throws Exception{
		WriteResult wr = mdd.update("{\"uId\" : " + uid + "}", "{\"lBills.Email\" : \""+email+"\"}");
		return wr;
	}
	
	/**
	 * 修改uid用户的登入密码为123456
	 * 
	 * @param uid
	 * @return WriteResult
	 * @throws Exception
	 */
	public WriteResult updatePwdToOriginal(int uid) throws Exception {
		WriteResult wr = mdd.update("{\"uId\" : " + uid + "}", "{\"lPas\" : \"8ymatou.com\"}");
		return wr;
	}
	
	/**
	 * 修改交易密码为abc123
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	public WriteResult updatetPasByUid(int uid) throws Exception {
		WriteResult wr = mdd.update("{\"uId\" : " + uid + "}", "{\"tPas\" : \"54E12B6B392FD98136162FE3F7343AD5\"}");
		return wr;
	}
	
	public WriteResult unlockUserByUid(int uId) throws Exception{
		WriteResult wr = mdd.update("{\"uId\" : " + uId + "}", "{\"Locked\" : false}");
		return wr;
	}
	
	/**
	 * @param ub
	 * @throws Exception
	 */
	public WriteResult saveULC(ULC201406Bean ub) throws Exception {
		return mdd.saveJSON(FormatUtil.beanToGSONString(ub));
	}

	/**
	 * @param findJson
	 * @return
	 * @throws Exception
	 */
	public WriteResult deleteData(String findJson) throws Exception {
		return mdd.delete(findJson);
	}

	public static void main(String args[]) throws Exception {
		ULC201406Wapper uw = new ULC201406Wapper();
		List<Integer> listuid = new ArrayList<Integer>();
		Date d1 =YMTDateUtil.getDate();
		Date d2 =YMTDateUtil.getDate();
		try {
			int size = 10;
			Logger.debug("开始插入数据");
			for (int i = 0; i < size; i++) {
				ULC201406Bean ub = new ULC201406Bean();
				ub.setcDate("2015-12-18T17:21:55.080+08:00");
				ub.setmDate("2015-12-18T17:21:55.080+08:00");
				LBills bls = new LBills();
				int uid = 50000000 + new Random().nextInt(90000000);
				bls.setLoginId("autotest_" + uid);
				ub.setlBills(bls);
				ub.setuId(uid);
				listuid.add(uid);
				ub.set_id(TestDataManager.getRandomUUID().toString().replace("-", ""));
				uw.saveULC(ub);
			}
			d2 =YMTDateUtil.getDate();
			Logger.debug("auto数据插入完成,耗时:" + YMTDateUtil.diffMs(d1, d2));
			Thread.sleep(1000);
			d1 =YMTDateUtil.getDate();
			Logger.debug("开始用uid循环查询数据");
			for (int uid : listuid) {
				uw.getULCInfoByUid(uid);
			}
			d2 =YMTDateUtil.getDate();
			Logger.debug("uid循环查询数据结束,耗时:" + YMTDateUtil.diffMs(d1, d2));
			d1 =YMTDateUtil.getDate();
			Logger.debug("开始循环更新数据");
			for (int uid : listuid) {
				uw.updatePwdByUid(uid);
			}
			d2 =YMTDateUtil.getDate();
			Logger.debug("循环更新数据结束,耗时:" + YMTDateUtil.diffMs(d1, d2));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Logger.debug("删除测试数据");
			d1 =YMTDateUtil.getDate();
			for (int uid : listuid) {
				uw.deleteData("{\"uId\":" + uid + "}");
			}
			d2 =YMTDateUtil.getDate();
			Logger.debug("删除测试数据完成,耗时:" + YMTDateUtil.diffMs(d1, d2));
		}
	}
	
	public void close() throws Throwable {
		mdd.close();
		mdd=null;
	}
}
